﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.COM;

namespace MINERP.DAL.Statistics
{
    public class BillEditDAL : DALBase
    {
        /// <summary>
        /// 获取未结帐账单信息
        /// </summary>
        /// <param name="inShopId"></param>
        /// <param name="inStartTime"></param>
        /// <param name="inEndTime"></param>
        /// <returns></returns>
        public List<SHOP_Bill> GetBillByShopAndTime(int inShopId, DateTime inStartTime, DateTime inEndTime)
        {
            //string shopSql = "select value it from  SHOP_Bill as it where it.Shop.ShopId="+inShopId+ "it.";
            int status = (int)ShopBillStatus.UnCheckOut;
            return DataEntites.SHOP_Bill.Where(p => p.Shop.ShopId == inShopId && p.CreateTime >= inStartTime && p.CreateTime <= inEndTime).ToList();
        }
        public SHOP_Bill GetBillByID(int inID)
        {
            //string shopSql = "select value it from  SHOP_Bill as it where it.Shop.ShopId="+inShopId+ "it.";
            int status = (int)ShopBillStatus.UnCheckOut;
            return DataEntites.SHOP_Bill.Where(p => p.BillId == inID).FirstOrDefault();
        }
        /// <summary>
        /// 获得账单产品
        /// </summary>
        /// <param name="inBillId"></param>
        /// <returns></returns>
        public List<Bill_Detail> GetListBill_Detail(int inBillId)
        {
            return DataEntites.Bill_Detail.Where(p => p.SHOP_Bill.BillId == inBillId).ToList();
        }
        /// <summary>
        /// 获得详细信息类表
        /// </summary>
        /// <param name="inBill_DetailId"></param>
        /// <returns></returns>
        public List<Bill_Detail_Detail> GetListBill_Detail_Detail(int inBill_DetailId)
        {
            return DataEntites.Bill_Detail_Detail.Where(p => p.Bill_Detail.BillDetailId == inBill_DetailId).ToList();
        }
        /// <summary>
        /// 获得产品信息
        /// </summary>
        /// <param name="inBarcode"></param>
        /// <returns></returns>
        public orderInfo GetProductByBarCode(string inBarcode)
        {
            return DataEntites.orderInfo.Where(p => p.OrderProductCode == inBarcode).FirstOrDefault();
        }
        /// <summary>
        /// 获取产品类型名称
        /// </summary>
        /// <param name="inId"></param>
        /// <returns></returns>
        public Tb_Product GetOneByProductId(int inId)
        {
            return DataEntites.Tb_Product.Where(p => p.ProuctId == inId).FirstOrDefault();
        }
        /// <summary>
        /// 获得收费明细列表
        /// </summary>
        /// <param name="inDetailId"></param>
        /// <returns></returns>
        public List<Bill_Detail_Detail> GetDetailDetail(int inDetailId)
        {
            return DataEntites.Bill_Detail_Detail.Where(p => p.Bill_Detail.BillDetailId == inDetailId).ToList();
        }
        /// <summary>
        /// 获得一条明细
        /// </summary>
        /// <param name="inDetailDetailId"></param>
        /// <returns></returns>
        public Bill_Detail_Detail GetDetailDetailOne(int inDetailDetailId)
        {
            return DataEntites.Bill_Detail_Detail.Where(p => p.Bill_Detail_DetailId == inDetailDetailId).FirstOrDefault();
        }


        /// <summary>
        /// 取得账单产品
        /// </summary>
        /// <param name="inDetailId"></param>
        /// <returns></returns>
        public Bill_Detail GetDetailOne(int inDetailId)
        {
            return DataEntites.Bill_Detail.Where(p => p.BillDetailId == inDetailId).FirstOrDefault();
        }
        /// <summary>
        /// 提交改变
        /// 
        /// </summary>
        public void Commit()
        {
            DataEntites.SaveChanges();
        }
        public void AddShopBill(SHOP_Bill inShopBill, int inBillDetailId)
        {
            foreach (var a in DataEntites.Bill_Detail.Where(p => p.BillDetailId == inBillDetailId))
            {
                inShopBill.Bill_Detail.Add(a);
            }
            DataEntites.AddToSHOP_Bill(inShopBill);
        }

        public void AddNewDetailDetail(Bill_Detail_Detail inDetail)
        {
            DataEntites.AddToBill_Detail_Detail(inDetail);
        }
        public void DelDetailDetail(Bill_Detail_Detail detail)
        {
            DataEntites.DeleteObject(detail);

        }

        public void DelDetail(Bill_Detail detail)
        {
            DataEntites.DeleteObject(detail);

        }

        public void InsertDetail(MINERP.MODEL.FoundationModel.Bill_Detail_Info inDetail, int inShopBillid)
        {
            Bill_Detail detail = new Bill_Detail();
            SHOP_Bill bill = DataEntites.SHOP_Bill.Where(p => p.BillId == inShopBillid).FirstOrDefault();
            detail.SHOP_Bill = bill;
            detail.OrderInfoCode = inDetail.OrderInfoCode;
            detail.OrderProductName = inDetail.OrderProductName;
            detail.OiRemark = inDetail.Remark;
            detail.CtmName = inDetail.CtmName;
            //  bill.TotalPrice+=
            DataEntites.AddToBill_Detail(detail);
            DataEntites.SaveChanges();
        }

        public void UpdataDetail(MINERP.MODEL.FoundationModel.Bill_Detail_Info inDetail)
        {
            Bill_Detail detail = DataEntites.Bill_Detail.Where(p => p.BillDetailId == inDetail.BillDetailId).FirstOrDefault(); ;
            detail.CtmName = inDetail.CtmName;
            detail.OiRemark = inDetail.Remark;
            detail.OrderInfoCode = inDetail.OrderInfoCode;
            detail.OrderProductName = inDetail.OrderProductName;
            //detail.

            DataEntites.SaveChanges();
        }

        public orderInfo_BK GetProductByCode(string inCode)
        {
            return DataEntites.orderInfo_BK.FirstOrDefault(p => p.OrderProductCode == inCode);
        }
        public SHOP_Bill GetBill(int inBillID)
        {
            return DataEntites.SHOP_Bill.Where(p => p.BillId == inBillID).FirstOrDefault();
        }


        public void DelBill(SHOP_Bill shopBill)
        {
            DataEntites.DeleteObject(shopBill);
        }
        //获取所有未结账的账单
        public List<SHOP_Bill> GetBillByShopAndTime(int inShopId, DateTime inDate)
        {
            List<SHOP_Bill> listBill = new List<SHOP_Bill>();
            int unPay = (int)ShopBillStatus.UnCheckOut;
            listBill = DataEntites.SHOP_Bill.Where(p => p.Shop.ShopId == inShopId && p.CreateTime.Month == inDate.Month && p.CreateTime.Year == inDate.Year && p.Status == unPay).ToList();
            return listBill;
        }
        /// <summary>
        /// 获取新的账单序号
        /// </summary>
        /// <param name="inShopID"></param>
        /// <param name="inDate"></param>
        /// <returns></returns>
        public int GetCodeNext(int inShopID, DateTime inDate)
        {
            List<SHOP_Bill> billList = DataEntites.SHOP_Bill.Where(p => p.Shop.ShopId == inShopID && p.CreateTime.Year == inDate.Year && p.CreateTime.Month == inDate.Month).ToList();
            SHOP_Bill bill = new SHOP_Bill();
            if (billList.Count == 0)
            {
                return 1;
            }
            else
            {
                return billList.Max(p => p.BillOrderCode) + 1;
            }
        }

        public Shop GetShopById(int inShopid)
        {
            return DataEntites.Shop.FirstOrDefault(p => p.ShopId == inShopid);
        }
        /// <summary>
        /// 修改转移账单修改相应的商户金额
        /// </summary>
        /// <param name="inFromShopId"></param>
        /// <param name="inToShopId"></param>
        /// <param name="inMoney"></param>
        /// <param name="strRemark"></param>
        /// <returns></returns>
        public int ChangeShopBill(int inFromShopId, int inToShopId, decimal inMoney, string strRemark,string inStrUserName)
        {
            try
            {


                Shop shopFrom = DataEntites.Shop.FirstOrDefault(p => p.ShopId == inFromShopId);
                Shop shopTo = DataEntites.Shop.FirstOrDefault(p => p.ShopId == inToShopId);
                if (inFromShopId != 0 && inToShopId != 0)//转移账单
                {
                    shopFrom.Balance = shopFrom.Balance + inMoney;//转出账单加上相应的金额
                    ShopBlanceCrushRecord recordFrom = new ShopBlanceCrushRecord();
                    recordFrom.CrushMoney = inMoney;
                    recordFrom.CrushTime = DateTime.Now;
                    recordFrom.Operation = (int)CountOperationType.ChongZhi;
                    recordFrom.Overage = (decimal)shopFrom.Balance;
                    recordFrom.Remark = "移走账单(产品)金额补偿";
                    recordFrom.Shop = shopFrom;
                    recordFrom.Operator = inStrUserName;
                    DataEntites.AddToShopBlanceCrushRecord(recordFrom);
                    shopTo.Balance = shopTo.Balance - inMoney;//转如账单减去相应的金额
                    ShopBlanceCrushRecord recordTo = new ShopBlanceCrushRecord();
                    recordTo.CrushMoney = -inMoney;
                    recordTo.CrushTime = DateTime.Now;
                    recordTo.Operation = (int)CountOperationType.DiChong;
                    recordTo.Overage = (decimal)shopTo.Balance;
                    recordTo.Remark = "添加账单(产品)金额抵扣";
                    recordTo.Shop = shopTo;
                    recordTo.Operator = inStrUserName;
                    DataEntites.AddToShopBlanceCrushRecord(recordTo);



                }
                else if (inFromShopId == 0 && inToShopId != 0)//修改账单
                {

                    shopTo.Balance = shopTo.Balance - inMoney;//修改明细
                    ShopBlanceCrushRecord recordTo = new ShopBlanceCrushRecord();
                    recordTo.CrushMoney = -inMoney;
                    recordTo.CrushTime = DateTime.Now;
                    recordTo.Operation = (int)CountOperationType.DiChong;
                    recordTo.Overage = (decimal)shopTo.Balance;
                    recordTo.Remark = strRemark;
                    recordTo.Shop = shopTo;
                    recordTo.Operator = inStrUserName;
                    DataEntites.AddToShopBlanceCrushRecord(recordTo);

                }
                else
                {

                }
                return 0;
            }
            catch (Exception exception)
            {
                throw exception;
               // return -1;
                
            }
        }
    }
}
